package com.jdrx.sw.sewage.beans.vo.base;

import com.github.pagehelper.Page;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页VO
 */
@Data
public class PageVO<T> {

	/**
	 * 默认分页
	 */
	static int PAGE_SIZE = 25;

	/**
	 * 当前页
	 */
	int pageNum;

	/**
	 * 每页条数
	 */
	int pageSize;

	/**
	 * 总条数
	 */

	long total;

	/**
	 * 数据对象
	 */
	List<T> data;

	public PageVO() {
	}

	public PageVO(Page page) {
		this.pageNum = page == null ? 1 : page.getPageNum();
		this.pageSize = page == null ? PAGE_SIZE : page.getPageSize();
		this.total = (page == null ? 0 : page.getTotal());
		this.data = (page == null || page.getResult() == null) ? new ArrayList<>(0) : page.getResult();
	}

	public void copyPageInfo(PageVO<?> pageInfo) {
		this.pageNum = pageInfo.getPageNum();
		this.pageSize = pageInfo.getPageSize();
		this.total = pageInfo.getTotal();
	}

	public static <V> PageVO<V> toPageVO(List<V> list) {
		PageVO<V> pageVo = new PageVO<>();
		if (list instanceof Page) {
			Page<V> page = (Page<V>) list;
			pageVo.setPageNum(page.getPageNum());
			pageVo.setPageSize(page.getPageSize());
			pageVo.setTotal(page.getTotal());
			pageVo.setData(list);
		} else {
			pageVo.setPageNum(0);
			pageVo.setPageSize(list.size());
			pageVo.setTotal(list.size());
			pageVo.setData(list);
		}
		return pageVo;
	}
}